﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using Tools;
using Model;
using DAL;
using BLL;
using System.Threading.Tasks;

namespace 下载写号
{
    public partial class DownloadAndWriteForm : Form
    {
        private AppHelper app = new AppHelper();
        private ConfigHelper configHelper = new ConfigHelper();
        private SerialPort relayPort = new SerialPort();
        private SerialPort boxPort = new SerialPort();
        private string relayPortName;
        private string boxPortName;
        private string setPath= System.Environment.CurrentDirectory + "\\Setting.config";
        private DownloadAndWriteFormBLL formBLL = new DownloadAndWriteFormBLL();
        private ML302DAL mL302DAL = new ML302DAL();
        private PortBLL portBLL = null;
        private UIBLL uIBLL = new UIBLL();
        private Dictionary<string, string> dic = new Dictionary<string, string>();
        private List<Sql.t_CMCC_ProdSN> prodList = new List<Sql.t_CMCC_ProdSN>();
        private Setting set = null;
        private List< string> result=new List<string>();
        private List<string> ngResult = new List<string>();
        private Thread th1 = null;
        private delegate void invokehandler();
        public DownloadAndWriteForm()
        {
            InitializeComponent();
        }

        public void PortInit()
        {
            comboBoxRelayPort.Items.Clear();
            comboBoxRelayPort.Items.AddRange(SerialPort.GetPortNames());
            comboBoxBoxPort.Items.Clear();
            comboBoxBoxPort.Items.AddRange(SerialPort.GetPortNames());
            relayPortName = dic[ "relayPort"];
            boxPortName= dic["boxPort"];
            if (relayPortName== boxPortName)
            {
                labelMes.Text = "串口配置错误！";
                return;
            }
            if (comboBoxRelayPort.Items.Contains(relayPortName))
            {
                if (!relayPort.IsOpen)
                {
                    portBLL.OpenPort(relayPort, relayPortName,115200);
                    comboBoxRelayPort.Text = relayPortName;
                    if (relayPort.IsOpen)
                    {
                        portBLL.SendData(relayPort, PortModel.PowerOFF);
                    }

                }
            }
            if (comboBoxBoxPort.Items.Contains(boxPortName))
            {
                if (!boxPort.IsOpen)
                {
                    portBLL.OpenPort(boxPort, boxPortName,115200);
                    comboBoxBoxPort.Text = boxPortName;
                    if (boxPort.IsOpen)
                    {
                        portBLL.SendData(boxPort, PortModel.ClearData);
                    }
                }
            }

        }
        private void DownloadAndWriteForm_Load(object sender, EventArgs e)
        {
            bool isOk = new Sql().SqlCheck();
            if (isOk == false)
            {
                MessageBox.Show("服务器连接异常！");
                this.Close();
                System.Environment.Exit(0);
            }
            portBLL = new PortBLL(textBoxRec,labelMes);
            this.Visible = false;
            dic = configHelper.ReadAll(setPath);
            label2.Text = "工位" + dic["devcode"];
            tabControl1.SelectedIndex = 1;
            tabPage2.Focus();
            panel2.Focus();
            app.GetAPP(this, panel2, "写号", dic);
            AppHelper.Delay(1000);
            app.MouseClick(PointModel.Simba_XH.MaxWindow);
            AppHelper.Delay(300);
            tabControl1.SelectedIndex = 0;
            tabPage1.Focus();
            panel1.Focus();
            app.GetAPP(this,panel1, "下载", dic);
            AppHelper.Delay(1000);
            app.KeyDown(Keys.Enter);
            PortInit();
            set = new Setting(dic);
        }
        private void labelMes_TextChanged(object sender, EventArgs e)
        {
            if (labelMes.Text.Contains("！") || labelMes.Text.Contains("!"))
            {
                labelMes.ForeColor = Color.Red;
            }
            else
            {
                labelMes.ForeColor = Color.Black;
            }
        }
        private void DownloadAndWriteForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Diagnostics.Process[] myproc = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process item in myproc)
            {
                if (item.ProcessName.Equals("Simba.data"))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
            System.Environment.Exit(0);
        }
        private void radioButtonManual_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonManual.Checked == true)
            {
                if (th1 != null)
                {
                    th1.Abort();
                    Thread.Sleep(100);
                    th1 = null;
                }
                btnStart.BackColor = tabPage1.BackColor;
                btnCode.BackColor = tabPage1.BackColor;
                btnDownload.BackColor = tabPage1.BackColor;
                btnWrite.BackColor = tabPage1.BackColor;
                btnTake.BackColor = tabPage1.BackColor;
                btnTake.Enabled = true;
                btnCode.Enabled = true;
                btnDownload.Enabled = true;
                btnWrite.Enabled = true;
                btnStart.Enabled = true;
                textBoxRec.Clear();

            }
        }
        private void radioButtonAuto_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonAuto.Checked == true)
            {
                btnStart.BackColor = tabPage1.BackColor;
                btnCode.BackColor = tabPage1.BackColor;
                btnDownload.BackColor = tabPage1.BackColor;
                btnWrite.BackColor = tabPage1.BackColor;
                btnTake.BackColor = tabPage1.BackColor;
                btnTake.Enabled = false;
                btnCode.Enabled = false;
                btnDownload.Enabled = false;
                btnWrite.Enabled = false;
                btnStart.Enabled = true;
                textBoxRec.Clear();
            }
        }

        #region 方法
        public void Place()
        {
            uIBLL.SetButton(btnStart, Color.Gold, false);
            portBLL.SendData(boxPort, PortModel.ClearData).Trim();
            AppHelper.Delay(100);
            uIBLL.LabelShowMsg(labelMes, "打开测试箱");
            if (!portBLL.SendData(boxPort, PortModel.OpenBox).Trim().Contains(PortModel.OpenBoxOk))
            {
                uIBLL.SetButton(btnStart, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes,"测试箱打开失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "关闭供电");
            if (!portBLL.SendData(relayPort, PortModel.PowerOFF).Trim().Contains( PortModel.PowerOFFOk))
            {
                uIBLL.SetButton(btnStart, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "继电器控制失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "请求放板");
            if (!portBLL.SendData(boxPort, PortModel.PlacePCB).Trim().Contains(PortModel.PlacePCBOk))
            {
                uIBLL.SetButton(btnStart, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "请求放板失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "放板完成");
            uIBLL.SetButton(btnStart, Color.ForestGreen, false);
            uIBLL.SetButton(btnCode, tabPage1.BackColor, true);
            AppHelper.Delay(50);
        }
        public void GetCode()
        {
            uIBLL.SetButton(btnCode, Color.Gold, false);
            uIBLL.LabelShowMsg(labelMes,  "获取条码");
            string codes = portBLL.SendData(boxPort, PortModel.GetCode);
            string[] code = codes.Split(';');
            string snimei = code[1] + ";" + code[2];
            string parentsn = mL302DAL.GetParentsn(labelMes, snimei);
            //string parentsn = mL302DAL.GetParentsn("863293051962113;2227AM002350CE018857");
            prodList = mL302DAL.GetProdList(labelMes,parentsn);
            if (prodList.Count != 8)
            {
                prodList.Clear();
                Sql.t_CMCC_ProdSN err = new Sql.t_CMCC_ProdSN() { snimei = codes };
                prodList.Add(err);
                uIBLL.SetButton(btnCode, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "条码查询失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "获取条码完成");
            uIBLL.LabelShowMsg(labelMes, "关闭测试箱");
            if (!portBLL.SendData(boxPort, PortModel.CloseBox).Trim().Contains( PortModel.CloseBoxOk))
            {
                uIBLL.SetButton(btnCode, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "测试箱关闭失败！");
                return;
            }
            uIBLL.SetButton(btnCode, Color.ForestGreen, false);
            uIBLL.SetButton(btnDownload, tabPage1.BackColor, true);
            AppHelper.Delay(50);
        }
        public void Download()
        {
            this.Invoke(new invokehandler(delegate ()
            {
                tabControl1.SelectedIndex = 0;
            }));
            uIBLL.SetButton(btnDownload, Color.Gold, false);
            uIBLL.LabelShowMsg(labelMes, "关闭供电");
            if (!portBLL.SendData(relayPort, PortModel.PowerOFF).Trim().Contains(PortModel.PowerOFFOk))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "继电器控制失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "切换状态到下载");
            if (!portBLL.SendData(relayPort, PortModel.SetToDownload).Trim().Contains( PortModel.SetToDownloadOk))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "切换状态到下载失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "下载程序");
            app.MouseClick(PointModel.Download.Start);
            uIBLL.LabelShowMsg(labelMes, "开启供电");
            if (!portBLL.SendData(relayPort, PortModel.PowerON_1).Trim() .Contains(PortModel.PowerONOk_1))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电1失败！");
                return;
            }
            AppHelper.Delay(600);
            if (!portBLL.SendData(relayPort, PortModel.PowerON_2).Trim().Contains(PortModel.PowerONOk_2))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电2失败！");
                return;
            }
            AppHelper.Delay(600);
            if (!portBLL.SendData(relayPort, PortModel.PowerON_3).Trim().Contains(PortModel.PowerONOk_3))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电3失败！");
                return;
            }
            AppHelper.Delay(600);
            if (!portBLL.SendData(relayPort, PortModel.PowerON_4).Trim().Contains(PortModel.PowerONOk_4))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电4失败！");
                return;
            }
            AppHelper.Delay(600);
            if (!portBLL.SendData(relayPort, PortModel.PowerON_5).Trim().Contains(PortModel.PowerONOk_5))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电5失败！");
                return;
            }
            AppHelper.Delay(600);
            if (!portBLL.SendData(relayPort, PortModel.PowerON_6).Trim().Contains(PortModel.PowerONOk_6))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电6失败！");
                return;
            }
            AppHelper.Delay(600);
            if (!portBLL.SendData(relayPort, PortModel.PowerON_7).Trim().Contains(PortModel.PowerONOk_7))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电7失败！");
                return;
            }
            AppHelper.Delay(600);
            if (!portBLL.SendData(relayPort, PortModel.PowerON_8).Trim().Contains(PortModel.PowerONOk_8))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "开启供电8失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "等待下载");
            AppHelper.Delay(37000);
            formBLL.WaitDownload(100);
            app.MouseClick(PointModel.Download.Stop);
            uIBLL.LabelShowMsg(labelMes, "关闭供电");
            if (!portBLL.SendData(relayPort, PortModel.PowerOFF).Trim().Contains(PortModel.PowerOFFOk))
            {
                uIBLL.SetButton(btnDownload, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "继电器控制失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "下载完成");
            uIBLL.SetButton(btnDownload, Color.ForestGreen, false);
            uIBLL.SetButton(btnWrite, tabPage1.BackColor, true);
            AppHelper.Delay(50);
        }
        public void Write()
        {
            this.Invoke(new invokehandler(delegate ()
            {
                tabControl1.SelectedIndex = 1;
            }));
            uIBLL.SetButton(btnWrite, Color.Gold, false);
            uIBLL.LabelShowMsg(labelMes, "关闭供电");
            if (!portBLL.SendData(relayPort, PortModel.PowerOFF).Trim().Contains( PortModel.PowerOFFOk))
            {
                uIBLL.SetButton(btnWrite, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "继电器控制失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "切换状态到写号");
            if (!portBLL.SendData(relayPort, PortModel.SetToWrite).Trim() .Contains( PortModel.SetToWriteOk))
            {
                uIBLL.SetButton(btnWrite, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "继电器控制失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "写号程序");
            formBLL.SetPorts(set, 4, app,this);
            AppHelper.Delay(200);
            List<String> codeList1 = new List<string>()
                {
                prodList[4].snimei,
                prodList[5].snimei,
                prodList[6].snimei,
                prodList[7].snimei,
                };
            formBLL.StarWrite(codeList1, app, 4, relayPort, portBLL,this);
            result.Clear();
            formBLL.GetWriteReslut(app, ref result, relayPort, portBLL);
            uIBLL.LabelShowMsg(labelMes, "关闭供电");
            if (!portBLL.SendData(relayPort, PortModel.PowerOFF).Trim().Contains(PortModel.PowerOFFOk))
            {
                uIBLL.SetButton(btnWrite, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "继电器控制失败！");
                return;
            }
            AppHelper.Delay(200);
            uIBLL.LabelShowMsg(labelMes, "写号程序");
            formBLL.SetPorts(set, 8, app,this);
            AppHelper.Delay(200);
            List<String> codeList2 = new List<string>()
                {
                prodList[0].snimei,
                prodList[1].snimei,
                prodList[2].snimei,
                prodList[3].snimei,
                };
            formBLL.StarWrite(codeList2, app, 8, relayPort, portBLL,this);
            formBLL.GetWriteReslut(app, ref result, relayPort, portBLL);
            uIBLL.LabelShowMsg(labelMes, "关闭供电");
            if (!portBLL.SendData(relayPort, PortModel.PowerOFF).Trim().Contains( PortModel.PowerOFFOk))
            {
                uIBLL.SetButton(btnWrite, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "继电器控制失败！");
                return;
            }
            uIBLL.LabelShowMsg(labelMes, "写号完成");
            uIBLL.SetButton(btnWrite, Color.ForestGreen, false);
            uIBLL.SetButton(btnTake, tabPage1.BackColor, true);
            AppHelper.Delay(50);
        }
        public void Take()
        {
            uIBLL.SetButton(btnTake, Color.Gold, false);
            uIBLL.LabelShowMsg(labelMes, "打开测试箱");
            if (!portBLL.SendData(boxPort, PortModel.OpenBox).Trim().Contains( PortModel.OpenBoxOk))
            {
                uIBLL.SetButton(btnTake, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "打开测试箱失败！");
                return;
            }
            List<string> returns = formBLL.ResultList(result, prodList);
            LogHelper.WriteLog(returns);
            uIBLL.LabelShowMsg(labelMes, "返回结果");
            Task t1, t2, t3, t4, t5, t6, t7, t8;
            t1 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[0], dic["devcode"]));
            t2 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[1], dic["devcode"]));
            t3 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[2], dic["devcode"]));
            t4 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[3], dic["devcode"]));
            t5 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[4], dic["devcode"]));
            t6 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[5], dic["devcode"]));
            t7 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[6], dic["devcode"]));
            t8 = Task.Factory.StartNew(() => mL302DAL.WriteSN(labelMes,returns[7], dic["devcode"]));
            for (int i = 0; i < returns.Count; i++)
            {
                if (returns[i].Contains("ng"))
                {
                    ngResult.Add(returns[i]);
                }
                else
                {
                    ngResult.Clear();
                }
                if (returns[i].Contains("err"))
                {
                    break;
                }
                if (i == 6)
                {
                    Task.WaitAll(t1, t2, t3, t4, t5, t6, t7, t8);
                }
                if (!portBLL.SendData(boxPort, returns[i]).Contains(PortModel.ResultOk))
                {
                    uIBLL.SetButton(btnTake, Color.Red, true);
                    uIBLL.LabelShowMsg(labelMes, "结果返回异常！");
                    return;
                }
            }
            if (!portBLL.GetData(boxPort).Contains(PortModel.TakeOk))
            {
                uIBLL.SetButton(btnTake, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "结果返回异常！");
                return;
            }
            prodList.Clear();
            if (ngResult.Count == 24)
            {
                uIBLL.SetButton(btnTake, Color.Red, true);
                uIBLL.LabelShowMsg(labelMes, "连续NG，请检查软件状态！！");
                ngResult.Clear();
                return;
            }
            uIBLL.LabelShowMsg(labelMes,"取板完成");
            uIBLL.SetButton(btnTake, Color.ForestGreen, false);
            AppHelper.Delay(50);
        }
        #endregion

        #region 调试Page
        #region 鼠标
        //private void GetMousePoint()
        //{
        //    Point ms = Control.MousePosition;
        //    this.label2.Text = string.Format("当前鼠标位置：{0}:{1}", ms.X, ms.Y);
        //    MouseButtons mb = Control.MouseButtons;

        //}
        ////更新鼠标位置等信息。
        //private void timer1_Tick(object sender, EventArgs e)
        //{
        //    GetMousePoint();
        //}
        #endregion
        #region 继电器
        private void buttonPowerON_Click(object sender, EventArgs e)
        {
            if (relayPort.IsOpen)
            {
                portBLL.SendData(relayPort, PortModel.PowerON);
            }
        }
        private void buttonPowerOFF_Click(object sender, EventArgs e)
        {
            if (relayPort.IsOpen)
            {
                portBLL.SendData(relayPort, PortModel.PowerOFF);
            }
        }

        private void buttonDownload_Click(object sender, EventArgs e)
        {
            if (relayPort.IsOpen)
            {
                portBLL.SendData(relayPort, PortModel.SetToDownload);
            }
        }

        private void buttonWrite_Click(object sender, EventArgs e)
        {
            if (relayPort.IsOpen)
            {
                portBLL.SendData(relayPort, PortModel.SetToWrite);
            }
        }
        #endregion
        #region 测试箱
        private void buttonOpen_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen)
            {
                portBLL.SendData(boxPort, PortModel.OpenBox);
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen)
            {
                portBLL.SendData(boxPort, PortModel.CloseBox);
            }
        }
        #endregion
        #region 机器人
        private void buttonClearDate_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen)
            {
                portBLL.SendData(boxPort, PortModel.ClearData);
            }
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen)
            {
                portBLL.SendData(boxPort, PortModel.InitRobot);
            }
        }
        private void buttonPut_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen)
            {
                portBLL.SendData(boxPort, PortModel.PlacePCB);
            }
        }
        private void buttonCode_Click(object sender, EventArgs e)
        {
            string [] code = portBLL.SendData(boxPort, PortModel.GetCode).Split(';');
            string snimei = code[1] + ";" + code[2];
            //string snimei = portBLL.SendData(boxPort, PortModel.GetCode).Substring(3);
            string parentsn = mL302DAL.GetParentsn(labelMes,snimei);
            prodList = mL302DAL.GetProdList(labelMes,parentsn);
        }
        private void buttonOKTake_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen && prodList.Count == 8)
            {
                List<string> returns = formBLL.ResultList(PortModel.AllOKResult, prodList);
                foreach (string item in returns)
                {
                    if (portBLL.SendData(boxPort, item).Contains(PortModel.ResultOk))
                    {
                        mL302DAL.WriteSN(labelMes,item, dic["devcode"]);
                    }
                }
                if (portBLL.GetData(boxPort).Contains(PortModel.TakeOk)) ;
                prodList.Clear();
            }
        }
        private void buttonNGTake_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen && prodList.Count == 8)
            {
                List<string> returns = formBLL.ResultList(PortModel.AllNGResult, prodList);
                foreach (string item in returns)
                {
                    if (portBLL.SendData(boxPort, item).Contains(PortModel.ResultOk))
                    {
                        mL302DAL.WriteSN(labelMes,item, dic["devcode"]);
                    }
                }
                if (portBLL.GetData(boxPort).Contains(PortModel.TakeOk)) ;
                prodList.Clear();
            }
        }
        #endregion
        #region 串口
        private void comboBoxRelayPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (relayPort.IsOpen)
            {
                relayPort.Close();
            }
            if (comboBoxRelayPort.Text != "")
            {
                portBLL.OpenPort(relayPort, comboBoxRelayPort.Text,115200);
            }
            if (comboBoxRelayPort.Text != relayPortName)
            {
                relayPortName = comboBoxRelayPort.Text;
                configHelper.Write(setPath, "relayPort", relayPortName);
                dic["relayPort"] = relayPortName;
            }
        }

        private void comboBoxBoxPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (boxPort.IsOpen)
            {
                boxPort.Close();
            }
            if (comboBoxBoxPort.Text != "")
            {
                portBLL.OpenPort(boxPort, comboBoxBoxPort.Text,115200);
            }
            if (comboBoxBoxPort.Text != boxPortName)
            {
                boxPortName = comboBoxBoxPort.Text;
                configHelper.Write(setPath, "boxPort", boxPortName);
                dic["boxPort"] = boxPortName;
            }
        }
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (boxPort.IsOpen && radioButtonBox.Checked == true)
            {
                string str = textBoxSend.Text;
                portBLL.SendData(boxPort, str);
            }
            if (relayPort.IsOpen && radioButtonRelay.Checked == true)
            {
                string str = textBoxSend.Text;
                portBLL.SendData(relayPort, str);
            }
        }
        private void comboBoxRelayPort_DropDown(object sender, EventArgs e)
        {
            comboBoxRelayPort.Items.Clear();
            comboBoxRelayPort.Items.AddRange(SerialPort.GetPortNames());
        }

        private void comboBoxBoxPort_DropDown(object sender, EventArgs e)
        {
            comboBoxBoxPort.Items.Clear();
            comboBoxBoxPort.Items.AddRange(SerialPort.GetPortNames());
        }

        private void textBoxRec_TextChanged(object sender, EventArgs e)
        {
            textBoxRec.SelectionStart = textBoxRec.TextLength;
            textBoxRec.ScrollToCaret();
        }
        #endregion
        #endregion
        #region 功能模块
        private void btnStart_Click(object sender, EventArgs e)
        {
            textBoxRec.Clear();
            if (!(boxPort.IsOpen && relayPort.IsOpen))
            {
                btnStart.BackColor = Color.Red;
                labelMes.Text = "串口未打开！";
                return;
            }
            if (radioButtonAuto.Checked == true)
            {
                try
                {
                    if (th1 == null)
                    {
                        th1 = new Thread(thread1);
                        th1.Start();
                    }
                }
                catch (Exception ee)
                {

                    throw;
                }
            }
            else
            {
                Place();
            }
        }
        private void btnCode_Click(object sender, EventArgs e)
        {
            GetCode();
        }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            Download();
        }
        private void btnWrite_Click(object sender, EventArgs e)
        {
            Write();
        }

        private void btnTake_Click(object sender, EventArgs e)
        {
            Take();
        }
        #endregion
        private void thread1()
        {

            while (true)
            {
                uIBLL.SetButton(btnStart, tabPage1.BackColor, true);
                uIBLL.SetButton(btnCode, tabPage1.BackColor, false);
                uIBLL.SetButton(btnDownload, tabPage1.BackColor, false);
                uIBLL.SetButton(btnWrite, tabPage1.BackColor, false);
                uIBLL.SetButton(btnTake, tabPage1.BackColor, false);
                Place();
                if (btnStart.BackColor == Color.Red)
                {
                    return;
                }
                GetCode();
                if (btnCode.BackColor == Color.Red)
                {
                    result = PortModel.AllNGResult;
                    Take();
                    if (btnTake.BackColor == Color.Red)
                    {
                        return;
                    }
                    continue;
                }
                Download();
                if (btnDownload.BackColor == Color.Red)
                {
                    result = PortModel.AllNGResult;
                    Take();
                    if (btnTake.BackColor == Color.Red)
                    {
                        return;
                    }
                    continue;
                }
                Write();
                if (btnWrite.BackColor == Color.Red)
                {
                    result = PortModel.AllNGResult;
                    Take();
                    if (btnTake.BackColor == Color.Red)
                    {
                        return;
                    }
                    continue;
                }
                Take();
                if (btnTake.BackColor == Color.Red)
                {
                    return;
                }
            }
        }
    }
}
